package com.sun.hunk.struts;

import com.sun.hunk.constant.CommonConstant;
import com.sun.hunk.constant.FormConstant;
import com.sun.hunk.constant.RequestConstant;
import com.sun.hunk.constant.SessionConstant;
import com.sun.hunk.constant.MessageConstant;
import com.sun.hunk.constant.JspPathConstant;
import com.sun.hunk.hibernate.BaseHibernateDAO;
import com.sun.hunk.hibernate.Domain;
import com.sun.hunk.util.CommonUtil;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author sunil
 */
public class HunkDispatchAction extends DispatchAction implements CommonConstant, RequestConstant, SessionConstant, MessageConstant, JspPathConstant,FormConstant {

    protected static final String INDEX = "index";
    protected static final String SUCCESS = "success";
    protected static final String SEARCH = "search";
    protected static final String INPUT = "input";
    protected BaseHibernateDAO baseHibernateDAO = new BaseHibernateDAO();

    public void setMessage(HttpServletRequest request, String message){
        request.setAttribute(MESSAGE, message);
    }
    public void setAlert(HttpServletRequest request, String message){
        request.setAttribute(ALERT, message);
    }
    public void setError(HttpServletRequest request, String message){
        request.setAttribute(ERROR, message);
    }
    public void setInfo(HttpServletRequest request, String message){
        request.setAttribute(INFO, message);
    }

    public boolean isLoggedInUser(HttpServletRequest request){
        return (CommonUtil.isNotNull(request.getSession().getAttribute(USER)));
    }

    public void save(Domain instance){
        baseHibernateDAO.save(instance);
    }

    public void saveOrUpdate(Domain instance){
        baseHibernateDAO.saveOrUpdate(instance);
    }

    public <T> T findById(Long id, Class _class){
        return (T) baseHibernateDAO.findById(id);
    }

    public void disablePage(HttpServletRequest request,boolean disable){
        request.setAttribute(DISABLE_PAGE, true);
    }
}
